home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / util / shell / csh540src.lha / main.c < prev    next >
C/C++ Source or Header  |  1995-01-31  |  18KB  |  747 lines

  1. /*
  2.  * MAIN.C
  3.  *
  4.  * Matthew Dillon, 24 Feb 1986
  5.  * (c)1986 Matthew Dillon     9 October 1986
  6.  *
  7.  * Version 2.07M by Steve Drew 10-Sep-87
  8.  * Version 4.01A by Carlo Borreo & Cesare Dieni 17-Feb-90
  9.  * Version 5.00L by U. Dominik Mueller 17-Feb-91
  10.  * Version 5.20L by Andreas M. Kirchwitz (Fri, 13 Mar 1992)
  11.  *
  12.  */
  13.  
  14. #include "shell.h"
  15.  
  16.  
  17. #define CSH_VER "5"
  18. #define CSH_REV "40"
  19.  
  20.  
  21. static struct Window *getwindow(void);
  22. static void exectimer(int stop);
  23. static void set_kickversion(void);
  24. char shellcompiled[]="Compiled: "__DATE__" "__TIME__" with "COMPILER"\n";
  25.  
  26. #if 1
  27. char shellname[]    ="Csh "CSH_VER"."CSH_REV" (public release)";
  28. #else
  29. char shellname[]    ="Csh "CSH_VER"."CSH_REV" (BETA)";
  30. #endif
  31.  
  32. char shellversion[] =CSH_VER""CSH_REV;
  33. char shellvers[]    =CSH_VER"."CSH_REV;
  34. char shellv   []    ="\0$VER: csh "CSH_VER"."CSH_REV" "__AMIGADATE__"";
  35. char shellctr []    ="CshCounter";
  36. char shellres []    ="CshResident";
  37. char shellthere[]   ="CshLoggedIn";
  38.  
  39. char *oldtitle = NULL;
  40. char trueprompt[100];
  41. char Inline[260];
  42. extern struct ExecBase *SysBase;    /* standard fare....*/
  43. extern struct DosLibrary *DOSBase;    /* more standard fare.... */
  44. struct IntuitionBase *IntuitionBase;
  45. struct GfxBase *GfxBase;
  46. struct Library *GadToolsBase;
  47. struct Library *AslBase;        /* AMK: Asl-FileRequester replaces ARP */
  48. struct Library *BattClockBase;
  49.  
  50. struct Window *old_WindowPtr = NULL;
  51. int    oldtaskpri = -999;
  52. BPTR   OldCin;
  53. void   *PatternBase;
  54. BOOL   nologout = FALSE;
  55. BOOL   nowintitle = FALSE;
  56. struct MsgPort *acs_oldport = NULL;    /* old MsgPort */
  57.  
  58. #ifdef DO_ACS_KLUDGE
  59. /*
  60.  * - New option "-K" (Kludge) for KingCON/ToolManager, sends DOS-Paket
  61.  *    ACTION_CHANGE_SIGNAL.  Don't use this option if you have no problems.
  62.  */
  63. int    acs_kludge = 0;            /* ACTION_CHANGE_SIGNAL kludge */
  64. #endif
  65.  
  66. #ifdef MULTIUSER_SUPPORT
  67. struct muBase *muBase;     /* LILJA: Added for multiuser-support */
  68. #endif
  69.  
  70. static char *defset[]={
  71.     v_histnum,  "0",
  72.     v_titlebar, shellname,
  73.     v_hist,     "50",
  74.     v_lasterr,  "0",
  75.     v_stat,     "0",
  76.     v_path,     "RAM:,RAM:c,df0:c,df1:c,sys:system,csh:,s:",
  77.     v_rxpath,   "REXX:",
  78.     v_scroll,   "2",
  79.     v_minrows,  "34",
  80.     v_hilite,   "c7",
  81.     v_lcd,      "",
  82.     v_qcd,      "csh:csh-qcd",
  83.     v_insert,   "1",
  84.     v_abbrev,   "5",
  85.     "_terminal","",
  86.     "_man",     "csh:csh.doc",
  87.     "_version", shellversion,
  88. /*    v_nomatch,  "1",*/
  89.     v_prghash,  "csh:csh-prgs",
  90.     NULL,       NULL
  91. };
  92.  
  93. static char *defalias[]={
  94.     "cls",  "echo -n ^l",
  95.     "dswap","cd $_lcd",
  96.     "exit", "endcli;quit",
  97.     "cdir", "%q cd $q; cls; dir",
  98.     "q",    "quit",
  99.     "rx",   "RX",
  100.     "manlist", "search -nl $_man \"    \"",
  101.     NULL,   NULL
  102. };
  103.  
  104. struct MsgPort *Console=(struct MsgPort *)-1;
  105. long ExecTimer, ExecRC;
  106.  
  107. #ifdef __SASC
  108. long __stack = 17500L;
  109. #endif
  110.  
  111. main(int argc, char *argv[])
  112. {
  113.     static ROOT locals;
  114.     BPTR fh;
  115.     int i, login=0;
  116.     char buf[10];
  117.     BOOL nologin=FALSE,nocshrc=FALSE,noclrmenu=FALSE;
  118. #ifdef AZTEC_C
  119.     extern int Enable_Abort;
  120.     Enable_Abort = 0;
  121. #endif
  122.  
  123.     MyMem=salloc(4);
  124.  
  125.     if( argc==0 ) {              /* Prevent starting from workbench */
  126.         Delay(60);
  127.         exit(0);
  128.     }
  129.  
  130.     Myprocess = (struct Process *)FindTask(NULL);
  131.     OldCin    = Myprocess->pr_CIS;
  132.     Mycli     = Cli();
  133. /*    Mycli     = (struct CommandLineInterface *)((long)Myprocess->pr_CLI << 2);*/
  134.  
  135.     init_mbase();
  136.     push_locals( &locals );
  137.     initmap();
  138.  
  139.     if (!SysBase || SysBase->LibNode.lib_Version<37) {
  140.         printf("Sorry, you'll need at least V37 to run CSH.\n");
  141.         exit(20);
  142.     }
  143.  
  144.     if (!DOSBase) {
  145.         printf("No dos library\n");
  146.         exit(20);
  147.     }
  148.  
  149.     /* we do not work without it ... */
  150.     DOSBase->dl_Root->rn_Flags |= RNF_WILDSTAR;
  151.  
  152.     if (!(GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",37L))) {
  153.         printf("No graphics library\n");
  154.         exit(20);
  155.     }
  156.  
  157.     if (!(IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",37L))) {
  158.         printf("No intuition library\n");
  159.         CloseLibrary((struct Library *)GfxBase);
  160.         exit(20);
  161.     }
  162.  
  163.     if (!(AslBase=OpenLibrary("asl.library",37L))) {
  164.         printf("No asl library\n");
  165.         CloseLibrary((struct Library *)IntuitionBase);
  166.         CloseLibrary((struct Library *)GfxBase);
  167.         exit(20);
  168.     }
  169.  
  170.     if (!(GadToolsBase=OpenLibrary("gadtools.library",37L))) {
  171.         printf("No gadtools library\n");
  172.         CloseLibrary((struct Library *)AslBase);
  173.         CloseLibrary((struct Library *)IntuitionBase);
  174.         CloseLibrary((struct Library *)GfxBase);
  175.         exit(20);
  176.     }
  177.  
  178. #ifdef MULTIUSER_SUPPORT
  179.     /* LILJA: Added for multiuser-support */
  180.     muBase = (struct muBase *)OpenLibrary("multiuser.library",39);
  181. #endif
  182.  
  183.     PatternBase = NULL; /* OpenLibrary("pattern.library",5L); */
  184.     BattClockBase = OpenResource(BATTCLOCKNAME);
  185.  
  186.     set_var(LEVEL_SET,"_kick2x",(SysBase->LibNode.lib_Version>=37)?"1":"0");
  187.     set_var(LEVEL_SET,"_kick3x",(SysBase->LibNode.lib_Version>=39)?"1":"0");
  188.  
  189.     set_kickversion();
  190.  
  191.     if( !IsInteractive(Input())) {
  192.         o_bground=1;
  193.         Mycli->cli_Background=DOSTRUE;
  194.     } else
  195.         Mycli->cli_Background=DOSFALSE;
  196.  
  197.     set_var( LEVEL_SET, v_bground, (o_bground) ? "1" : "0" );
  198.  
  199.     /* We don't really need "CONSOLE:", maybe good for stderr ... */
  200.     if( fh=Open( "CONSOLE:" , MODE_NEWFILE) ) {
  201.         Console= ((struct FileHandle *)(4*fh))->fh_Type;
  202.         Close(fh);
  203.     }
  204.  
  205.     Forbid();
  206.     i=0;
  207.     /* AMK: OS20-GetVar replaces ARP-Getenv, SetVar replaces Setenv */
  208.     if ( GetVar(shellres  ,buf,10,GVF_GLOBAL_ONLY|GVF_BINARY_VAR) >= 0L)
  209.         o_resident=1;
  210.     if ( GetVar(shellthere,buf,10,GVF_GLOBAL_ONLY|GVF_BINARY_VAR) <  0L)
  211.         login=TRUE, SetVar(shellthere,"1",-1L,GVF_GLOBAL_ONLY|GVF_BINARY_VAR);
  212.     if ( GetVar(shellctr  ,buf,10,GVF_GLOBAL_ONLY|GVF_BINARY_VAR) >= 0L)
  213.         i=atoi(buf);
  214.     sprintf(buf, "%d", i+1);
  215.     /* AMK: OS20-SetVar replaces ARP-Setenv */
  216.     SetVar(shellctr, buf, -1L, GVF_GLOBAL_ONLY|GVF_BINARY_VAR);
  217.     Permit();
  218.  
  219. #ifdef AZTEC_C
  220.     stdin->_flags    |= _IONBF;    /* make sure we're set as an unbuffered tty */
  221.     stdout->_flags    |= _IONBF;    /* in case of redirection in .login */
  222.     Close( (BPTR)_devtab[2].fd);
  223.     _devtab[2].mode |= O_STDIO;
  224.     _devtab[2].fd = _devtab[1].fd;    /* set stderr to Output() otherwise */
  225.                     /* don't work with aux driver */
  226. #else
  227.     /* if( setvbuf( stdout,NULL,_IOLBF,BUFSIZ )) exit(20); */
  228.     /* setnbf( stdout ); */
  229.     /* Close( _ufbs[2] );*/
  230.     /*_ufbs[2]=_ufbs[1]; */
  231.     /* setnbf( stderr ); */
  232. #endif
  233.  
  234.     sprintf(buf,"%ld",Myprocess->pr_TaskNum);
  235.     set_var(LEVEL_SET, "_clinumber", buf);
  236.  
  237.     seterr(0);
  238.     if (Myprocess->pr_CurrentDir == NULL)
  239.         execute("cd :");
  240.     set_cwd();
  241.  
  242.     o_nowindow= 1;
  243.  
  244.     set_var(LEVEL_SET,v_prompt, (IsInteractive(Input())) ? "%c%p> ":"");
  245.  
  246.     for( i=0; defset[i]; i+=2 )
  247.         set_var( LEVEL_SET, defset[i], defset[i+1] );
  248.     for( i=0; defalias[i]; i+=2 )
  249.         set_var( LEVEL_ALIAS, defalias[i], defalias[i+1] );
  250.  
  251.     o_nowindow= 0;
  252.  
  253.     for (i = 1; i < argc; ++i) {
  254.         if (*argv[i]=='-' && (index(argv[i],'a') || index(argv[i],'t')))
  255.             o_nowindow=1;
  256.         if (*argv[i]=='-' && index(argv[i],'n'))
  257.             nologin = TRUE;
  258.         if (*argv[i]=='-' && index(argv[i],'N'))
  259.             nocshrc = TRUE;
  260.         if (*argv[i]=='-' && index(argv[i],'L'))
  261.             nologout = TRUE;
  262.         if (*argv[i]=='-' && index(argv[i],'M'))
  263.             noclrmenu = TRUE;
  264.         if (*argv[i]=='-' && index(argv[i],'W'))
  265.             nowintitle = TRUE;
  266.         if (*argv[i]=='-' && index(argv[i],'R'))
  267.             o_noraw = 1;
  268.         if (*argv[i]=='-' && index(argv[i],'w'))
  269.             o_nowindow = 1;
  270.         if (*argv[i]=='-' && (index(argv[i],'C') || index(argv[i],'c'))) {
  271.             noclrmenu  = TRUE;
  272.             nowintitle = TRUE;
  273.         }
  274.     }
  275.  
  276.     if (Mycli->cli_Background) {
  277.         o_noraw    = 1;
  278.         o_nowindow = 1;
  279.         o_vt100    = 1;
  280.     }
  281.  
  282.     if( !o_nowindow && (Mywindow=getwindow()) && IsInteractive(Input())) {
  283.         old_WindowPtr = Myprocess->pr_WindowPtr;
  284.         Myprocess->pr_WindowPtr = 0L/*Mywindow*/;
  285.         newwidth();
  286.         if (!nowintitle)
  287.             oldtitle=(char *)(Mywindow->Title);
  288.         if (!noclrmenu) {
  289.             /* clear menus, even if we are not the owner */
  290.             set_menu();
  291.         }
  292.     }
  293.  
  294.     if (login && !nologin) {
  295.         /*printf("we're the first csh today, mon ami!\n");*/
  296.         if( exists("S:.login"))
  297.             execute("source S:.login");
  298.     }
  299.  
  300.     if (!nocshrc) {
  301.         if( exists("S:.cshrc"))
  302.             execute("source S:.cshrc");
  303.     }
  304.  
  305.     {
  306.     char nam1[40],nam2[40],nam3[40];
  307.     BOOL e1,e2,e3;
  308.     strcpy(nam1,"ENVARC:"); strcat(nam1,shellctr);   e1=exists(nam1);
  309.     strcpy(nam2,"ENVARC:"); strcat(nam2,shellres);   e2=exists(nam2);
  310.     strcpy(nam3,"ENVARC:"); strcat(nam3,shellthere); e3=exists(nam3);
  311.  
  312.     if (e1 || e2 || e3) {
  313.         printf("\nWARNING: please remove the following files from ENVARC:\n\n");
  314.         if (e1)
  315.         printf("          - %s\n",shellctr);
  316.         if (e2)
  317.         printf("          - %s\n",shellres);
  318.         if (e3)
  319.         printf("          - %s\n",shellthere);
  320.         printf("\n         and reboot your system after removing the files.\n");
  321.         printf("         Never copy ENV: to ENVARC: !!\n\n");
  322.         Delay(150L);
  323.     }
  324.     }
  325.  
  326.     /* would be a nice idea to have a flag that tells if this is
  327.        a full shell or just executing some commands and the leaving */
  328.  
  329.     for (i = 1; i < argc; ++i) {
  330.         if (*argv[i]=='-') {
  331.             char *str=argv[i]+1;
  332.             for( ; *str; str++ ) {
  333.                 switch( *str) {
  334.                 case 'c':
  335.                 case 'C': execute( compile_av( argv, i+1, argc, ' ',*str=='C'));
  336.                           main_exit(Lastresult); break;
  337.                 case 'k': set_var(LEVEL_SET,v_nobreak,"1"); break;
  338. #ifdef DO_ACS_KLUDGE
  339.                 case 'K': acs_kludge = 1; break;
  340. #endif
  341.                 case 'v': Verbose=1; set_var(LEVEL_SET,v_verbose,"hs"); break;
  342.                 case 'b': oldtaskpri=Myprocess->pr_Task.tc_Node.ln_Pri;
  343.                           SetTaskPri( &Myprocess->pr_Task, -1 ); break;
  344.                 case 'f': oldtaskpri=Myprocess->pr_Task.tc_Node.ln_Pri;
  345.                           SetTaskPri( &Myprocess->pr_Task,  1 ); break;
  346.                 case 'a': o_nowindow= o_noraw= 1;
  347.                           set_var( LEVEL_SET, v_hilite, "" ); break;
  348. #if 0
  349.                 case 'm': unset_var(LEVEL_SET,v_nomatch); break;
  350. #endif
  351.                 case 'm': set_var(LEVEL_SET,v_nomatch,"1"); break;
  352.                 case 't': o_nowindow= o_vt100= o_nofastscr= 1;
  353.                           set_var( LEVEL_SET, v_hilite, "b" /*"r"*/ );
  354.                           set_var( LEVEL_SET, v_noreq, "1" );
  355.                           set_var( LEVEL_SET, "_terminal", "1" );
  356.                           set_var( LEVEL_ALIAS, "cls", "e -n ^[[0\\;0H^[[J" );
  357.                           break;
  358.                 case 's': DOSBase->dl_Root->rn_Flags |= RNF_WILDSTAR;
  359.                           break;
  360.                 case 'w': /* just don't use window pointer */
  361.                           o_nowindow = 1;
  362.                           set_var( LEVEL_SET, "_timeout", "1" ); /* GWB_TIMEOUT_LOCAL */
  363.                           break;
  364.                 case 'V': /* strict VT100 mode */
  365.                           o_vt100 = 1;
  366.                           break;
  367.                 }
  368.             }
  369.         } else {
  370.             sprintf (Inline, "source %s",argv[i]);
  371.             execute (Inline);
  372.         }
  373.     }
  374.  
  375.     /* we should do "main_exit(Lastresult);" here if 
  376.        csh was called with some files to source ... */
  377.  
  378. #if 0
  379.     if( fh=Open( "CONSOLE:" , MODE_NEWFILE) ) {
  380.         if (Console != ((struct FileHandle *)(4*fh))->fh_Type)
  381.             Write(fh,"Funny Console!\n",15);
  382.         else
  383.             Write(fh,"Real Console!\n",14);
  384.         Close(fh);
  385.     }
  386.     else
  387.         printf("No Console!\n");
  388.  
  389.     Write(Input(),"Input()!\n",9);
  390.     Write(Output(),"Output()!\n",10);
  391. #endif
  392.  
  393.     if( !IsInteractive(Input()) ) {
  394.         main_exit( Lastresult );
  395.     }
  396.  
  397. #ifdef DO_ACS_KLUDGE
  398.     if (acs_kludge>0) {
  399.         BPTR bfh = Open("*", MODE_OLDFILE);
  400.         struct FileHandle *fh;
  401.         long xrslt,xioerr;
  402.         if (bfh != NULL) {
  403.             fh = BADDR(bfh);
  404.             if (fh->fh_Type != NULL) {
  405.                 xrslt = DoPkt( (void *)fh->fh_Type, ACTION_CHANGE_SIGNAL, fh->fh_Arg1, (long)&(Myprocess->pr_MsgPort), NULL,NULL,NULL);
  406.                 /* also possible instead of fh->fh_Type: Myprocess->pr_ConsoleTask */
  407.                 xioerr = IoErr();
  408.                 acs_oldport = (struct MsgPort *)xioerr;
  409.                 printf("ACS_1: DoPkt()=%ld, fh_Type=%ld, ConTask=%ld, pr_MsgPort=%ld, IoErr()=%ld\n",xrslt,(long)fh->fh_Type,(long)Myprocess->pr_ConsoleTask,(long)&(Myprocess->pr_MsgPort),xioerr);
  410.             }
  411.             Close(bfh);
  412.         }
  413.         acs_kludge = 2;
  414.     }
  415. #endif
  416.  
  417.     for (;;) {
  418.         if (breakcheck())
  419.             while (WaitForChar(Input(), 100L) || CHARSWAIT( stdin ))
  420.                 gets(Inline);
  421.         clearerr(stdin);  /* prevent acidental quit */
  422.         /*
  423.            AMK: new position of breakreset() before exec_every(),
  424.                 old position caused enforcer hits
  425.                 (do 'set _every "set cols = $(wincols)"' and
  426.                  then abort (^c) a command)
  427.         */
  428.         breakreset();
  429.         exec_every();
  430.  
  431.         { /* AMK TEST BEGIN */
  432.             char *old;
  433.             char pwd[256];
  434.  
  435.             /* no requesters when no disk is inserted */
  436.             Myprocess->pr_WindowPtr = (APTR)(-1);
  437.  
  438. #if 1
  439.             if (NameFromLock(Myprocess->pr_CurrentDir, pwd, 255L)) {
  440.                 if( !(old=get_var(LEVEL_SET, v_cwd)) )
  441.                     old="";
  442.                 if (strcmp(pwd,old)) {
  443.                     set_var(LEVEL_SET, v_lcd, old);
  444.                     set_cwd();
  445.                 }
  446.             }
  447. #else
  448.             if (!NameFromLock(Myprocess->pr_CurrentDir, pwd, 255L)) {
  449.                 fprintf(stderr,"csh.main: NameFromLock() failed\n");
  450.                 strcpy(pwd,"<unknown>");
  451.             }
  452.             if( !(old=get_var(LEVEL_SET, v_cwd)) )
  453.                 old="";
  454.             if (strcmp(pwd,old)) {
  455.                 set_var(LEVEL_SET, v_lcd, old);
  456.                 set_cwd();
  457.             }
  458. #endif
  459.             /*update_sys_var(v_cwd);*/
  460.  
  461.             Myprocess->pr_WindowPtr = o_noreq ? (APTR) -1L : 0L/*Mywindow*/;
  462.  
  463.         } /* AMK TEST BEGIN */
  464.  
  465.         update_sys_var(v_titlebar);
  466.         update_sys_var(v_prompt);
  467.         /*
  468.            AMK: old position of breakreset(), causes enforcer hits
  469.                 (do 'set _every "set cols = $(wincols)"' and
  470.                  then abort (^c) a command)
  471.         */
  472.         breakreset();
  473. #if RAW_CONSOLE
  474.         if (Quit || !rawgets(Inline, disable ? "_ " : trueprompt)) main_exit(0);
  475. #else
  476.         printf("%s", disable ? "_ " : trueprompt);
  477.         fflush(stdout);
  478.         if (Quit || !gets(Inline)) main_exit(0);
  479. #endif
  480.         breakreset();
  481.  
  482. #if 1
  483.         /* start/stop timer only if non-empty command line */
  484.  
  485.         if (*Inline && strlen(Inline)>0) {
  486.             exectimer(0);
  487.             ExecRC = exec_command(Inline);
  488.             exectimer(1);
  489.         }
  490. #else
  491.         /* start/stop timer for every command  */
  492.  
  493.         exectimer(0);
  494.         if (*Inline) ExecRC=exec_command(Inline);
  495.         exectimer(1);
  496. #endif
  497.     }
  498. }
  499.  
  500.  
  501. void
  502. main_exit(n)
  503. {
  504.     int i;
  505.     char buf[10];
  506.  
  507.     /* AMK: OS20-GetVar replaces ARP-Getenv */
  508.     GetVar(shellctr,buf,10L,GVF_GLOBAL_ONLY|GVF_BINARY_VAR);
  509.     i=atoi(buf);
  510.     sprintf(buf,"%d",i-1);
  511.     /* AMK: OS20-SetVar replaces ARP-Setenv */
  512.     SetVar(shellctr, buf, -1L, GVF_GLOBAL_ONLY|GVF_BINARY_VAR);
  513.  
  514. #ifdef DO_ACS_KLUDGE
  515.     if (acs_kludge>0) {
  516.         BPTR bfh = Open("*", MODE_OLDFILE);
  517.         struct FileHandle *fh;
  518.         long xrslt,xioerr;
  519.         if (bfh != NULL) {
  520.             fh = BADDR(bfh);
  521.             if (fh->fh_Type != NULL) {
  522.                 xrslt = DoPkt( (void *)fh->fh_Type, ACTION_CHANGE_SIGNAL, fh->fh_Arg1, (long)acs_oldport, NULL,NULL,NULL);
  523.                 /* also possible instead of fh->fh_Type: Myprocess->pr_ConsoleTask */
  524.                 xioerr = IoErr();
  525.                 printf("ACS_2: DoPkt()=%ld, fh_Type=%ld, ConTask=%ld, oldport=%ld, IoErr()=%ld\n",xrslt,(long)fh->fh_Type,(long)Myprocess->pr_ConsoleTask,(long)acs_oldport,xioerr);
  526.             }
  527.             Close(bfh);
  528.         }
  529.         acs_kludge = 0;
  530.     }
  531. #endif
  532.  
  533.     if( !nowintitle && oldtitle )
  534.         SetWindowTitles(Mywindow,oldtitle,(char *)-1);
  535.     if( oldtaskpri != -999 )
  536.         SetTaskPri(&Myprocess->pr_Task,oldtaskpri);
  537.  
  538.     if (Mywindow)
  539.         Myprocess->pr_WindowPtr = old_WindowPtr;
  540.  
  541.     for (i=1; i<MAXMYFILES; i++) myclose(i);
  542.  
  543.     /* no effect if AnzMenus==0 (no menus in use) */
  544.     remove_menu();
  545.  
  546. #ifdef MULTIUSER_SUPPORT
  547.     /* LILJA: Added for multiuser-support */
  548.     if (muBase) CloseLibrary((struct Library *)muBase);
  549. #endif
  550.  
  551.     if (PatternBase) CloseLibrary( PatternBase );
  552.     CloseLibrary((struct Library *)GadToolsBase);
  553.     CloseLibrary((struct Library *)AslBase);
  554.     CloseLibrary((struct Library *)IntuitionBase);
  555.     CloseLibrary((struct Library *)GfxBase);
  556.     exit(n);
  557. }
  558.  
  559.  
  560. static void
  561. exectimer(int stop)
  562. {
  563.     struct DateStamp dss;
  564.     static long lasttime;
  565.     long time;
  566.  
  567.     DateStamp(&dss);
  568.     time=dss.ds_Minute*6000+2*dss.ds_Tick;   /* 2 = 100/TickPerSec   */
  569.     if( stop )
  570.         ExecTimer=time-lasttime;
  571.     else
  572.         lasttime=time;
  573. }
  574.  
  575.  
  576. /* print break message only once */
  577. static BOOL dobreak_output = TRUE;
  578.  
  579. int
  580. breakcheck()
  581. {
  582.     return !o_nobreak && SetSignal(0L,0L) & SIGBREAKF_CTRL_C;
  583. }
  584.  
  585. void
  586. breakreset()
  587. {
  588.     /* We reset CTRL_E here for GMD's foreach() hack ... */
  589.     /* Why not just reset CTRL_F also -- for completeness? :) */
  590.  
  591.     SetSignal(0L, SIGBREAKF_CTRL_C|SIGBREAKF_CTRL_D|SIGBREAKF_CTRL_E);
  592.     dobreak_output = TRUE;
  593. }
  594.  
  595. dobreak()
  596. {
  597.     if (breakcheck()) {
  598.         if (dobreak_output) {
  599.             printf("^C\n");
  600.             dobreak_output = FALSE;
  601.         }
  602.         return(1);
  603.     }
  604.     return(0);
  605. }
  606.  
  607. /* this routine causes manx to use this Chk_Abort() rather than it's own */
  608. /* otherwise it resets our ^C when doing any I/O (even when Enable_Abort */
  609. /* is zero).  Since we want to check for our own ^C's             */
  610.  
  611. long
  612. Chk_Abort()
  613. {
  614.     return(0);
  615. }
  616.  
  617. void
  618. _wb_parse()
  619. {
  620. }
  621.  
  622. do_howmany()
  623. {
  624.     char buf[10];
  625.  
  626.     /* AMK: OS20-GetVar replaces ARP-Getenv */
  627.     GetVar(shellctr, buf, 10L, GVF_GLOBAL_ONLY|GVF_BINARY_VAR);
  628.     printf("Shell(s) running: %s\n",buf);
  629.     return 0;
  630. }
  631.  
  632. static void set_kickversion(void)
  633. {
  634.     struct Library *verlib;
  635.     static char verstr[10];
  636.     if (verlib = OpenLibrary("version.library",0)) {
  637.         sprintf(verstr,"%d",verlib->lib_Version);
  638.         CloseLibrary(verlib);
  639.     }
  640.     else
  641.         sprintf(verstr,"%d",SysBase->LibNode.lib_Version);
  642.     set_var(LEVEL_SET,"_kick",verstr);
  643. }
  644.  
  645. #if 1
  646. static struct Window *
  647. getwindow(void)
  648. {
  649.     struct Window *win = NULL;
  650.  
  651.     if( o_nowindow )
  652.         return NULL;
  653.  
  654.     if( IsInteractive(Output()) )
  655.         Write(Output(),"",1);     /*    make window appear */
  656.  
  657.     if( isconsole(Output()) && Output() ) {
  658.         if ( ((struct FileHandle *)BADDR(Output()))->fh_Type ) {
  659.             struct InfoData *infodata;
  660.             infodata=(void *)SAllocMem((long)sizeof(struct InfoData),MEMF_CLEAR|MEMF_PUBLIC);
  661.             if (DoPkt((void *)((struct FileHandle *)BADDR(Output()))->fh_Type,ACTION_DISK_INFO,((LONG)infodata)>>2,NULL,NULL,NULL,NULL)) {
  662.                 win=(struct Window *)infodata->id_VolumeNode;
  663.             }
  664.             FreeMem(infodata,sizeof(struct InfoData));
  665.         }
  666.         if( win==NULL )
  667.             o_nowindow=1;
  668.     }
  669.  
  670.     newwidth();
  671.  
  672.     return win;
  673. }
  674. #else
  675. static struct Window *
  676. getwindow(void)
  677. {
  678.     struct Window *win = NULL;
  679.  
  680.     if( o_nowindow )
  681.         return NULL;
  682.     if( isconsole(Output()))
  683.         Write(Output(),"",1);     /*    make window appear */
  684.     if( Myprocess->pr_ConsoleTask ) {
  685.         struct InfoData *infodata;
  686.         infodata=(void *)SAllocMem((long)sizeof(struct InfoData),MEMF_CLEAR|MEMF_PUBLIC);
  687.         if (DoPkt((void *)Myprocess->pr_ConsoleTask,ACTION_DISK_INFO,((LONG)infodata)>>2,NULL,NULL,NULL,NULL)) {
  688.             win=(struct Window *)infodata->id_VolumeNode;
  689.         }
  690.         FreeMem(infodata,sizeof(struct InfoData));
  691.         if( win==NULL )
  692.             o_nowindow=1;
  693.     }
  694.     newwidth();
  695.     return win;
  696. }
  697. #endif
  698.  
  699. #ifdef LATTICE
  700.  
  701. int
  702. setenv( char *var, char *val )
  703. {
  704.     char *buf=salloc(strlen(var)+strlen(val)+10);
  705.     sprintf(buf, "%s=%s", var, val );
  706.     Free(buf);
  707.     return putenv(buf);
  708. }
  709.  
  710. __regargs int __chkabort(void) { return(0); }
  711.  
  712. #endif
  713.  
  714. #ifdef _DCC
  715. void
  716. swapmem(void *_s1, void *_s2, size_t _n)
  717. {
  718.     char t, *s1=_s1, *s2=_s2;
  719.     int  i;
  720.  
  721.     for( i=0; i<_n; i++ )
  722.         t = *s1; *s1++ = *s2; *s2++=t;
  723. }
  724.  
  725. int
  726. setenv( char *var, char *val )
  727. {
  728.     BPTR fh;
  729.     char buf[40];
  730.     sprintf(buf,"ENV:%s",var );
  731.  
  732.     if( fh=Open(buf,MODE_NEWFILE) ) {
  733.         Write(fh,val,strlen(val));
  734.         Close(fh);
  735.     }
  736. }
  737.  
  738. int
  739. rand()
  740. {
  741.     return 0;
  742. }
  743.  
  744. _waitwbmsg() {return 0;};
  745.  
  746. #endif
  747.